package com.yanqu.road.server.cmd.cookboy.process2.cross;

import com.yanqu.road.entity.activity.cookboy.process2.CookBoy2Spoon;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_COOK_BOY2_JOIN_JOINER, desc = "跨服参加者返回加奖励")
public class CrossJoinTableJoinerCmd extends AbstractCommand {

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		CookBoyProto.CrossCookBoy2JoinTableMsg crossRespMsg = CookBoyProto.CrossCookBoy2JoinTableMsg.parseFrom(packet.getBytes());
		GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(crossRespMsg.getJoinerUserId());
		if(gamePlayer == null){
			log.error("小当家2:"+packet.getPlayerId()+"{消息返回玩家无了}");
		}
		ret = crossRespMsg.getRet();
		CookBoyProto.CookBoy2JoinTableRespMsg.Builder respMsg = CookBoyProto.CookBoy2JoinTableRespMsg.newBuilder();
		CookBoy2Spoon cookBoy2Spoon = CookBoyActivityMgr.getCookBoy2Spoon(crossRespMsg.getActivityId(),crossRespMsg.getSpoonId());
		player.getModule(PreReduceModule.class).restorePreReduce(cookBoy2Spoon.getConsume());
		respMsg.setRet(ret);
		int autoJoin = crossRespMsg.hasAutoJoin() ? crossRespMsg.getAutoJoin() : 0; // 自动回访标识
		if(ret == 0){
			String reward = CookBoyMgr.joinerBackMsg(crossRespMsg.getActivityId(),packet.getPlayerId(),
					crossRespMsg.getSpoonId(), autoJoin);
			if (autoJoin == 0) {
				respMsg.setReward(reward);
			}
		}
		respMsg.addAllForbidSpoonId(crossRespMsg.getForbidSpoonIdList());
		gamePlayer.sendPacket(Protocol.U_COOK_BOY2_JOIN_TABLE, respMsg);

		if(ret == 0 && crossRespMsg.hasOtherActivityId() && crossRespMsg.getActivityId() != crossRespMsg.getOtherActivityId()){
			//互宴记录，写到自己活动
			CookBoyProto.CrossCookBoy2JoinDataSync.Builder joinMsg = CookBoyProto.CrossCookBoy2JoinDataSync.newBuilder();
			joinMsg.setActivityId(crossRespMsg.getActivityId());
			joinMsg.setOtherActivityId(crossRespMsg.getOtherActivityId());
			joinMsg.setJoinUserId(packet.getPlayerId());
			joinMsg.setHomeUserId(crossRespMsg.getHomeUserId());
			gamePlayer.sendPacket(Protocol.C_CROSS_COOK_BOY2_SYNC_JOIN_RECORD, joinMsg);
		}
	}

	@Override
	public void sendData(GamePlayer player) {

	}
}
